package com.xhj.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.*;
import com.xhj.config.AliyunVODProperties;
import org.springframework.beans.factory.annotation.Autowired;

public class VODUtils {

    /**
     * 上传视频，并返回响应结果
     * @param accessKeyId
     * @param accessKeySecret
     * @param title
     * @param fileName
     * @return
     * @throws Exception
     */
    public static CreateUploadVideoResponse uploadVideo(String accessKeyId, String accessKeySecret,String title,String fileName) throws Exception {
        DefaultAcsClient client = initVodClient(accessKeyId, accessKeySecret);
        return createUploadVideo(client, title, fileName);
    }

    /**
     * 获取播放地址
     * @param accessKeyId
     * @param accessKeySecret
     * @param videoId
     * @return
     * @throws Exception
     */
    public static GetPlayInfoResponse getPlayInfo(String accessKeyId, String accessKeySecret,String videoId) throws Exception {
        DefaultAcsClient client = initVodClient(accessKeyId, accessKeySecret);
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId(videoId);
        return client.getAcsResponse(request);
    }

    /**
     * 获取播放凭证
     * @param accessKeyId
     * @param accessKeySecret
     * @param videoId
     * @return
     * @throws Exception
     */
    public static GetVideoPlayAuthResponse getVideoPlayAuth(String accessKeyId, String accessKeySecret,String videoId) throws Exception {
        DefaultAcsClient client = initVodClient(accessKeyId, accessKeySecret);
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoId);
        return client.getAcsResponse(request);
    }
    /**
     * 初始化VOD对象
     * @param accessKeyId
     * @param accessKeySecret
     * @return
     * @throws ClientException
     */
    public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
        String regionId = "cn-shenzhen";  // 点播服务接入区域
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        return new DefaultAcsClient(profile);
    }

    /**
     * 创建VOD响应对象
     * @param client
     * @param title
     * @param fileName
     * @return
     * @throws Exception
     */
    private static CreateUploadVideoResponse createUploadVideo(DefaultAcsClient client,String title,String fileName) throws Exception {
        CreateUploadVideoRequest request = new CreateUploadVideoRequest();
        request.setTitle(title);
        request.setFileName(fileName);


        //UserData，用户自定义设置参数，用户需要单独回调URL及数据透传时设置(非必须)
        //JSONObject userData = new JSONObject();

        //UserData回调部分设置
        //JSONObject messageCallback = new JSONObject();
        //messageCallback.put("CallbackURL", "http://192.168.0.0/16");
        //messageCallback.put("CallbackType", "http");
        //userData.put("MessageCallback", messageCallback.toJSONString());

        //UserData透传数据部分设置
        //JSONObject extend = new JSONObject();
        //extend.put("MyId", "user-defined-id");
        //userData.put("Extend", extend.toJSONString());

        //request.setUserData(userData.toJSONString());

        return client.getAcsResponse(request);
    }

}
